<template>
  <ContentWrap>
    <div class="tongue-basic-config">
      <!-- 品牌开关 -->
      <el-form label-position="left" label-width="120px">
        <el-form-item label="品牌开关">
          <el-switch v-model="formData.brandSwitch" />
        </el-form-item>

        <!-- 购买方式 -->
        <el-form-item label="购买方式:">
          <div class="purchase-methods">
            <div class="purchase-method">
              <el-checkbox v-model="formData.purchaseTypes.year">年卡</el-checkbox>
              <div class="purchase-inputs">
                <div class="input-item">
                  <span class="input-label">价格</span>
                  <el-input v-model="formData.yearPrice" />
                </div>
                <div class="input-item">
                  <span class="input-label">次数</span>
                  <el-input v-model="formData.yearCount" />
                </div>
              </div>
            </div>
          </div>
        </el-form-item>

        <!-- 小程序路径和H5链接 -->
        <div class="url-row">
          <div class="url-item">
            <span class="text-right">小程序路径地址</span>
            <el-input v-model="formData.miniappParams" />
            <el-button>复制</el-button>
          </div>
        </div>
        <div class="url-row">
          <div class="url-item">
            <span class="text-right">H5 路径</span>
            <el-input v-model="formData.h5Path2" />
            <el-button>复制</el-button>
          </div>
        </div>

        <!-- 赠送开关 -->
        <el-form-item label="赠送开关:">
          <el-switch v-model="formData.giftSwitch" />
          <span class="switch-desc">用户界面可将次数赠送给好友</span>
        </el-form-item>

        <!-- 新用户免费赠送次数 -->
        <el-form-item label="新用户免费赠送次数:">
          <div class="input-with-unit">
            <el-input v-model="formData.newUserGiftCount" style="width: 200px;" />
            <span class="unit">次</span>
          </div>
        </el-form-item>

        <!-- 会员等级和赠送次数 -->
        <div class="member-levels">
          <div v-for="(level, index) in memberLevels" :key="index" class="member-level-item">
            <div class="member-level-row">
              <div class="member-level-select">
                <span>会员等级:</span>
                <el-select v-model="level.levelId" class="!w-150px" placeholder="请选择会员等级">
                  <el-option label="普通会员" value="1" />
                  <el-option label="银牌会员" value="2" />
                  <el-option label="金牌会员" value="3" />
                </el-select>
              </div>
              
              <div class="gift-count">
                <span>赠送次数:</span>
                <el-input v-model="level.giftCount" style="width: 150px;" />
                <span>次</span>
              </div>
              
              <div class="level-actions">
                <el-button type="primary" plain size="small" v-if="index === memberLevels.length - 1" @click="addMemberLevel">新增</el-button>
                <el-button type="danger" plain size="small" v-if="index !== 0" @click="removeMemberLevel(index)">删除</el-button>
              </div>
            </div>
          </div>
        </div>

        <!-- 隐私协议开关 -->
        <el-form-item label="隐私协议">
          <el-switch v-model="formData.privacySwitch" />
        </el-form-item>

        <!-- 隐私协议内容 -->
        <div class="editor-section" v-if="formData.privacySwitch">
          <div class="editor-title">隐私协议内容</div>
          <div class="editor-container">
            <Editor v-model="formData.privacyContent" :height="300" />
          </div>
        </div>

        <!-- 智能咨询介绍 -->
        <div class="editor-section">
          <div class="editor-title">智能舌面珍介绍页</div>
          <div class="editor-container">
            <Editor v-model="formData.aiConsultContent" :height="300" />
          </div>
        </div>

        <!-- 购买协议开关 -->
        <el-form-item label="购买协议">
          <el-switch v-model="formData.purchaseAgreementSwitch" />
        </el-form-item>

        <!-- 协议名称 -->
        <el-form-item label="协议名称" v-if="formData.purchaseAgreementSwitch">
          <el-input v-model="formData.agreementName" style="width: 500px;" />
        </el-form-item>

        <!-- 协议内容 -->
        <div class="editor-section" v-if="formData.purchaseAgreementSwitch">
          <div class="editor-title">协议内容</div>
          <div class="editor-container">
            <Editor v-model="formData.agreementContent" :height="300" />
          </div>
        </div>

        <!-- 提交按钮 -->
        <div class="form-actions">
          <el-button type="primary" @click="submitForm">提交</el-button>
        </div>
      </el-form>
    </div>
  </ContentWrap>
</template>

<script setup lang="ts">
import { reactive, ref,toRefs } from 'vue'
import { ElMessage } from 'element-plus'
import { Editor } from '@/components/Editor'

// 表单数据
const formData = reactive({
  brandSwitch: false,
  purchaseTypes: {
    year: false,
    month: false,
    count: false
  },
  yearPrice: '',
  yearCount: '',
  monthPrice: '',
  monthCount: '',
  countPrice: '',
  countNumber: '1',
  miniappPath: '',
  miniappParams: '',
  h5Path: '',
  h5Path2: '',
  giftSwitch: false,
  newUserGiftCount: '',
  privacySwitch: false,
  privacyContent: '',
  aiConsultContent: '',
  purchaseAgreementSwitch: false,
  agreementName: '',
  agreementContent: ''
})

// 会员等级列表
const memberLevels = ref([
  { levelId: '1', giftCount: '' }
])

// 添加会员等级
const addMemberLevel = () => {
  memberLevels.value.push({
    levelId: '',
    giftCount: ''
  })
}

// 删除会员等级
const removeMemberLevel = (index: number) => {
  if (index !== 0) { // 第一个不能删除
    memberLevels.value.splice(index, 1)
  }
}

// 提交表单
const submitForm = () => {
  // 表单验证逻辑
  if (!formData.purchaseTypes.year && !formData.purchaseTypes.month && !formData.purchaseTypes.count) {
    ElMessage.warning('请至少选择一种购买方式')
    return
  }

  // 会员等级验证
  for (const level of memberLevels.value) {
    if (!level.levelId) {
      ElMessage.warning('请选择会员等级')
      return
    }
  }

  // 提交表单逻辑
  ElMessage.success('配置保存成功')
}
</script>

<style scoped lang="scss">
.tongue-basic-config {
  padding: 20px 0;

  .el-form {
    padding: 24px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgb(0 0 0 / 5%);
  }

  .el-form-item {
    margin-bottom: 22px;
  }

  .purchase-methods {
    display: flex;
    flex-direction: column;
    gap: 18px;
    margin-bottom: 10px;
  }

  .purchase-method {
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 12px;
    background-color: #f8f9fa;
    border-radius: 6px;

    .el-checkbox {
      width: 70px;
    }

    .purchase-inputs {
      display: flex;
      gap: 20px;
    }

    .input-item {
      display: flex;
      align-items: center;
      gap: 10px;

      .input-label {
        width: 40px;
        color: #606266;
      }

      .el-input {
        width: 150px;
      }
    }
  }

  .url-row {
    display: flex;
    margin-bottom: 20px;
    gap: 20px;

    .url-item {
      flex: 1;
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 12px;
      background-color: #f8f9fa;
      border-radius: 6px;

      span {
        font-size: 14px;
        color: #606266;
        white-space: nowrap;
      }

      .el-input {
        flex: 1;
      }

      .el-button {
        margin-left: 5px;
      }
    }
  }

  .switch-desc {
    margin-left: 12px;
    font-size: 14px;
    color: #909399;
  }

  .input-with-unit {
    display: flex;
    align-items: center;
    gap: 12px;

    .unit {
      color: #606266;
    }
  }

  .member-levels {
    padding: 16px;
    margin: 24px 0;
    background-color: #f8f9fa;
    border-radius: 8px;

    .member-level-item {
      padding: 16px;
      margin-bottom: 16px;
      background-color: #fff;
      border-radius: 6px;
      box-shadow: 0 1px 4px rgb(0 0 0 / 5%);
    }

    .member-level-row {
      display: flex;
      align-items: center;
      gap: 20px;
    }

    .member-level-select, 
    .gift-count {
      display: flex;
      align-items: center;
      gap: 12px;

      span {
        min-width: 70px;
        font-size: 14px;
        color: #606266;
      }
    }

    .level-actions {
      margin-left: auto;
    }
  }

  .editor-section {
    padding: 20px;
    margin: 30px 0;
    background-color: #f8f9fa;
    border-radius: 8px;

    .editor-title {
      padding-bottom: 12px;
      margin-bottom: 16px;
      font-size: 16px;
      font-weight: 600;
      color: #303133;
      border-bottom: 1px solid #ebeef5;
    }

    .editor-container {
      overflow: hidden;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
    }
  }

  .form-actions {
    display: flex;
    padding-top: 24px;
    margin-top: 40px;
    border-top: 1px solid #ebeef5;
    justify-content: center;

    .el-button {
      padding: 12px 30px;
      font-size: 16px;
    }
  }
}
</style>